<?php
	
	function dbconnect() 
	{ 
		include('config.php'); 
		static $connect = null; 
		if ($connect === null) 
			{
				 $connect =  mysql_connect($host, $user, $passwd) or die(mysql_error());
				 mysql_query("SET character_set_results=utf8", $connect);
				 mysql_select_db($database); 
			} 
		return $connect; 
	} 
	
	function getbyid_hoadon($id)
	{
		$result= mysql_query("SELECT * FROM Hoa_don WHERE ID=N'".$id."'",dbconnect());
		return mysql_fetch_array($result); 
	}
	function getbyidnguoidung_hoadon($id)
	{
		$allnxb_req= mysql_query("SELECT Hoa_don.*, Nguoi_dung.Ho_va_ten, Tinh_trang_hoa_don.Ten_tinh_trang FROM Hoa_don 
		INNER JOIN Nguoi_dung
		ON Hoa_don.ID_Nguoi_dung = Nguoi_dung.ID
		INNER JOIN Tinh_trang_hoa_don
		ON Tinh_trang_hoa_don.ID = Hoa_don.ID_Tinh_trang_hoa_don
		WHERE ID_Nguoi_dung=N'".$id."'
		ORDER BY ID DESC",dbconnect());
		$result = array(); 
		while ($nxb = mysql_fetch_array($allnxb_req))
		 { 
			$result[] = $nxb; 
		 } 
		return $result; 
	}
	
	
	function getbyid_nguoidung($id)
	{
		$result= mysql_query("SELECT nguoi_dung.*, tinh_thanh_pho.Ten_tinh_thanh_pho, quan_huyen.Ten_quan_huyen 
		FROM nguoi_dung 
		INNER JOIN tinh_thanh_pho ON nguoi_dung.ID_Tinh_thanh_pho = tinh_thanh_pho.ID
		INNER JOIN quan_huyen ON nguoi_dung.ID_Quan_huyen = quan_huyen.ID
		WHERE Nguoi_dung.ID=N'".$id."'",dbconnect());
		return mysql_fetch_array($result); 
	}
	function getbyid_ctx($id)
	{
		$allnxb_req= mysql_query("SELECT * FROM Chi_tiet_hoa_don WHERE ID_Hoa_don=N'".$id."'",dbconnect());
		$result = array(); 
		while ($nxb = mysql_fetch_array($allnxb_req))
		 { 
			$result[] = $nxb; 
		 } 
		return $result;  
	}
	
	
	
	function getbyall_tthd()
	{ 
		$allnxb_req = mysql_query("SELECT * FROM Tinh_trang_hoa_don" ,dbconnect()); 
		$result = array(); 
		while ($nxb = mysql_fetch_array($allnxb_req))
		 { 
			$result[] = $nxb; 
		 } 
		return $result; 
	} 
	
	function getbyall_sach()
	{ 
		$allnxb_req = mysql_query("SELECT * FROM sach" ,dbconnect()); 
		$result = array(); 
		while ($nxb = mysql_fetch_array($allnxb_req))
		 { 
			$result[] = $nxb; 
		 } 
		return $result; 
	} 
	
	function update_tthd($post)
	{
	$query= mysql_query("UPDATE Hoa_don SET ID_Tinh_trang_hoa_don=N'".$post['ID_Tinh_trang_hoa_don']."' WHERE ID=N'".$post['ID']."' ",dbconnect()); 	
	}
	
	function getbyid_ttdh($id)
	{
		$result= mysql_query("SELECT * FROM Tinh_trang_hoa_don WHERE ID=N'".$id."'",dbconnect());
		return mysql_fetch_array($result);
	}
	
	
	function getbyid_sach($id)
	{
		$result= mysql_query("SELECT * FROM Sach WHERE ID=N'".$id."'",dbconnect());
		return mysql_fetch_array($result); 
	}
	
	function update_soluong($postx,$posty)
	{
	$query= mysql_query("UPDATE Sach SET So_luong=N'".$postx."' WHERE ID=N'".$posty."' ",dbconnect()); 	
	}
	
	function update_thongtinthem($x,$y)
	{
	$query= mysql_query("UPDATE Hoa_don SET Thong_tin_them=N'".$x."' WHERE ID=N'".$y."' ",dbconnect()); 	
	}
	
	function insert_cthd($idsach,$soluong,$giaban,$gianhap,$idhoadon,$thanhtien,$laisuat)
	{
		$query = mysql_query("INSERT INTO chi_tiet_hoa_don SET 
		ID_Sach =N'".$idsach."',
		So_luong =N'".$soluong."',
		Gia_ban =N'".$giaban."',
		Gia_nhap =N'".$gianhap."',
		ID_Hoa_don =N'".$idhoadon."', 
		Thanh_tien =N'".$thanhtien."',
		Lai_suat =N'".$laisuat."' ");
		}
	function getbyid_giaban($id)
	{
		$result= mysql_query("SELECT * FROM gia_ban WHERE (ID_Sach='".$id."' and Thoi_gian<now()) order by Thoi_gian DESC limit 1",dbconnect());
		return mysql_fetch_array($result); 
	}
	function getbyid_gianhap($id)
	{
		$result= mysql_query("SELECT * FROM Chi_tiet_nhap WHERE (ID_Sach='".$id."' ) order by ID DESC limit 1",dbconnect());
		return mysql_fetch_array($result); 
	}
	
?>